package com.sx.gongzhong.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZYG on 2017/6/23.
 * Viewpager+Fragment的适配器 绑定TabLayout
 */
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments=new ArrayList<Fragment>();
    private List<String> titles;

    public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
        super(fm);
        this.fragments=fragments;
        this.titles=titles;
    }

    @Override
    public Fragment getItem(int position) {
        if(!fragments.isEmpty()){
            return fragments.get(position);
        }
        return null;
    }

    @Override
    public int getCount() {
        if(!fragments.isEmpty()){
            return fragments.size();
        }
        return 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (!titles.isEmpty()) {
            return titles.get(position);
        }
        return super.getPageTitle(position);
    }
}
